package com.ihome.android.academy;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.ihome.android.EAActivity;
import com.ihome.android.EAApp;
import com.ihome.android.annotation.EAInjectView;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.image.ImageLoader;
import com.ihome.android.image.ImageLoaderFactory;
import com.ihome.android.image.impl.DefaultImageLoadHandler;
import com.ihome.android.academy.R;
import com.ihome.android.market2.asynctask.GetAppRankingTask;
import com.ihome.android.market2.asynctask.GetViewAppDetailTask;
import com.ihome.android.market2.info.TemplateContentInfo;
import com.ihome.android.market2.view.LogoTop;
import com.ihome.android.market2.view.RankingItem;
import com.ihome.android.util.log.LogUtil;

public class RankingActivity extends EAActivity implements OnItemClickListener {
	private String				TAG				= "RankingActivity";
	private final int			FIRST_RANK		= 0x01;
	private final int			SECOND_RANK		= 0x02;
	private final int			THIRD_RANK		= 0x03;

	@EAInjectView(id = R.id.user_topline)
	private LogoTop				user_topline;
	@EAInjectView(id = R.id.rank_first_title)
	TextView					firtRankTitle;
	@EAInjectView(id = R.id.rank_second_title)
	TextView					secondRankTitle;
	@EAInjectView(id = R.id.rank_third_title)
	TextView					thirdRankTitle;
	@EAInjectView(id = R.id.rank_first)
	ListView					firtRankView;
	@EAInjectView(id = R.id.rank_second)
	ListView					secondRankView;
	@EAInjectView(id = R.id.rank_third)
	ListView					thirdRankView;
	List<TemplateContentInfo>	firstListInfo	= new ArrayList<TemplateContentInfo>();
	List<TemplateContentInfo>	secodListInfo	= new ArrayList<TemplateContentInfo>();
	List<TemplateContentInfo>	thirdListInfo	= new ArrayList<TemplateContentInfo>();

	RankItemAdapter				firstAdapt		= null;
	RankItemAdapter				secondAdapt		= null;
	RankItemAdapter				thirdAdapt		= null;

	private int					sectionId		= 0;
	/** 加载图片 */
	private ImageLoader			imageLoader;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ranking);
		sectionId = getIntent().getExtras().getInt(HttpKeys.KEY_SECTIONID);
		LogUtil.d(TAG, "onCreate() --> sectionId = " + sectionId);

		imageLoader = ImageLoaderFactory.create(this);
		((DefaultImageLoadHandler) imageLoader.getImageLoadHandler()).setLoadingResources(R.drawable.default_recomm_item);
		initView();
		initData();
	}

	private void initView() {
		firstAdapt = new RankItemAdapter(this, FIRST_RANK);
		secondAdapt = new RankItemAdapter(this, SECOND_RANK);
		thirdAdapt = new RankItemAdapter(this, THIRD_RANK);

		firtRankView.setAdapter(firstAdapt);
		secondRankView.setAdapter(secondAdapt);
		thirdRankView.setAdapter(thirdAdapt);

		firtRankView.setOnItemClickListener(this);
		secondRankView.setOnItemClickListener(this);
		thirdRankView.setOnItemClickListener(this);
		
		user_topline.setUpdateAppNum(EAApp.getApp().mUpdateAppList.size());
	}

	private void initData() {
		getAppRankingListInfo();
	}

	public void getAppRankingListInfo() {
		LogUtil.d(TAG, "------getAppDetail()------");
		Bundle b = new Bundle();
		b.putInt(HttpKeys.KEY_SECTIONID, sectionId);
		new GetAppRankingTask(RankingActivity.this).execute(b);
	}

	public void fillUpRankTitle(int type, String title) {
		switch (type) {
		case FIRST_RANK:
			firtRankTitle.setText(title);
			break;
		case SECOND_RANK:
			secondRankTitle.setText(title);
			break;
		case THIRD_RANK:
			thirdRankTitle.setText(title);
			break;
		default:
			break;
		}
	}

	/**
	 * 关联应用数据请求成功后的回调方法
	 * 
	 * @param mListAppInfo
	 */
	public void fillUpRankinglist(int type, List<TemplateContentInfo> mListAppInfo) {
		if (mListAppInfo != null) {
			switch (type) {
			case FIRST_RANK:
				firstListInfo = mListAppInfo;
				firstAdapt.notifyDataSetChanged();
				firtRankView.requestFocus();
				break;
			case SECOND_RANK:
				secodListInfo = mListAppInfo;
				secondAdapt.notifyDataSetChanged();
				break;
			case THIRD_RANK:
				thirdListInfo = mListAppInfo;
				thirdAdapt.notifyDataSetChanged();
				break;
			default:
				break;
			}
		}
	}

	public void toDetailPage(int appId){
		Intent intent = new Intent();
		intent.putExtra(HttpKeys.KEY_APPID, appId);
		intent.setClass(RankingActivity.this, DetailActivity.class);
		startActivity(intent);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		TemplateContentInfo rankInfo = (TemplateContentInfo) parent.getItemAtPosition(position);
		/*		Toast.makeText(this, "appName=" + rankInfo.getName(), Toast.LENGTH_SHORT).show();*/
		try {
			Bundle b = new Bundle();
			b.putInt(HttpKeys.KEY_APPID, rankInfo.getContentId());
			b.putString(HttpKeys.KEY_PATHID, "");
			new GetViewAppDetailTask(RankingActivity.this,rankInfo.getContentId()).execute(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class RankItemAdapter extends BaseAdapter {
		private Context	adapterContext;
		private int		type;

		public RankItemAdapter(Context context, int type) {
			this.adapterContext = context;
			this.type = type;
		}

		private int getCount(List list) {
			int i = 0;
			if (list != null && list.size() > 0) {
				i = list.size();
			}
			else {
				i = 1;
			}
			return i;
		}

		private Object getsubItem(List list, int index) {
			if (list != null && list.size() > 0) {
				return list.get(index);
			}
			else {
				return null;
			}
		}

		@Override
		public int getCount() {
			int i = 7;
			switch (type) {
			case FIRST_RANK:
				i = getCount(firstListInfo);
				break;
			case SECOND_RANK:
				i = getCount(secodListInfo);
				break;
			case THIRD_RANK:
				i = getCount(thirdListInfo);
				break;
			default:
				break;
			}

			return i;
		}

		@Override
		public Object getItem(int arg0) {
			Object obj = null;
			switch (type) {
			case FIRST_RANK:
				obj = getsubItem(firstListInfo, arg0);
				break;
			case SECOND_RANK:
				obj = getsubItem(secodListInfo, arg0);
				break;
			case THIRD_RANK:
				obj = getsubItem(thirdListInfo, arg0);
				break;
			default:
				break;
			}

			return obj;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			RankingItem rankItem;
			TemplateContentInfo appInfo = (TemplateContentInfo) getItem(position);
			if (null == convertView) {
				rankItem = new RankingItem(adapterContext, imageLoader);
				convertView = rankItem.getView();
				convertView.setTag(rankItem);
			}
			else {
				rankItem = (RankingItem) convertView.getTag();
			}

			rankItem.initView(appInfo, position);
			return convertView;
		}
	}
}
